package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcWarnType;
import com.soft.idc.common.util.JQPage;

public class WarnTypeManagerController extends Controller {
	public void index() {
		this.render("/warn_manager/warn_type/warnType.html");
	}
	
	public void queryWarnType() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		StringBuffer sb = new StringBuffer();
		String name = this.getPara("name");
		sb.append("select t.warn_type_id, t.name, t.level  ");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("from idc_warn_type t where 1=1  ");
		if(name !=null ){
			sqlFrom.append(" and t.name like '%" + name + "%' ");
		}
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
	}
	
	public void add() {
		String name = this.getPara("name");
		String level = this.getPara("level");
		IdcWarnType warnType = new IdcWarnType();
		warnType.setName(name);
		if(level!=null && level.length()>0){
			warnType.setLevel(Integer.parseInt(level));
		}
		boolean result = warnType.save();
		this.renderJson(result);
	}
	
	public void delete() {
		final String typeIds = this.getPara("warnTypeIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] typeIdsArr = typeIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from idc_warn_type where warn_type_id in(");
				for(int i=0; i<typeIdsArr.length; i++){
					if(i==typeIdsArr.length-1){
						sb.append(typeIdsArr[i] + ")");
					}else{
						sb.append(typeIdsArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==typeIdsArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}
	
	public void edit() {
		boolean result = false;
		int warnTypeId = this.getParaToInt("warnTypeId");
		String name = this.getPara("name");
		String level = this.getPara("level");
		IdcWarnType warnType = new IdcWarnType();
		warnType = warnType.findById(warnTypeId);
		if(warnType!=null){
			warnType.setName(name);
			if(level!=null && level.length()>0){
				warnType.setLevel(Integer.parseInt(level));
			}
			result = warnType.update();
		}
		this.renderJson(result);
	}
}
